/*
 * Copyright (c) 2023 Hunan OpenValley Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import util from '@ohos.util';
import Queue from '@ohos.util.Queue';

export class ConvertUtils {
  /**
   * 十进制转二进制
   * @param num
   * @returns
   */
  static decimalToBinary(num: number): string {
    return num.toString(2);
  }

  /**
   * 二进制转十进制
   * @param binary
   * @returns
   */
  static binaryToDecimal(binary: string): number {
    return parseInt(binary, 2);
  }

  /**
   * 十进制转八进制
   * @param num
   * @returns
   */
  static decimalToOctal(num: number): string {
    return num.toString(8);
  }

  /**
   * 八进制转十进制
   * @param octal
   * @returns
   */
  static octalToDecimal(octal: string): number {
    return parseInt(octal, 8);
  }

  /**
   * 十进制转十六进制
   * @param num
   * @returns
   */
  static decimalToHexadecimal(num: number): string {
    return num.toString(16).toUpperCase();
  }

  /**
   * 十六进制转十进制
   * @param hexadecimal
   * @returns
   */
  static hexadecimalToDecimal(hexadecimal: string): number {
    return parseInt(hexadecimal, 16);
  }

  /**
   * 字节数组转十六进制字符串
   * @param byteArray
   * @returns
   */
  static bytes2HexString(byteArray: Uint8Array): string {
    return Array.from(byteArray, (byte: number) => {
      return ('0' + (byte & 0xff).toString(16)).slice(-2);
    }).join('').toUpperCase();
  }

  /**
   * 十六进制字符串转字节数组
   * @param hexString
   * @returns
   */
  static hexString2ByteArray(hexString: string): Uint8Array {
    const byteArray = new Uint8Array(hexString.length / 2);
    for (let i = 0; i < hexString.length; i += 2) {
      byteArray[i / 2] = parseInt(hexString.substr(i, 2), 16);
    }
    return byteArray;
  }

  /**
   * Uint8Array转字符串
   * @param uint8Array
   * @returns
   */
  static uint8Array2String(uint8Array: Uint8Array): string {
    const decoder = util.TextDecoder.create();
    return decoder.decodeWithStream(uint8Array);
  }

  /**
   * 字符串转Uint8Array
   * @param uint8Array
   * @returns
   */
  static string2Uint8Array(str: string): Uint8Array {
    const encoder = new util.TextEncoder();
    return encoder.encodeInto(str);
  }

  /**
   * 根据单元长度unitSize切割array成Uint8Array队列
   * @param array
   * @param unitSize
   * @returns
   */
  static split(array: Uint8Array, unitSize: number): Queue<Uint8Array> {
    const result: Queue<Uint8Array> = new Queue<Uint8Array>();
    const length: number = array.length;

    for (let i = 0; i < length; i += unitSize) {
      if (i + unitSize > length) {
        unitSize = length - i; // 最后一个unitSize需要动态计算
      }
      let subArray = new Uint8Array(unitSize);
      subArray.set(array.subarray(i, i + unitSize)); // const subArray: Uint8Array = array.subarray(i, i + unitSize);解决Uint8Array.subarray的buffer长度异常问题
      result.add(subArray);
    }

    return result;
  }
}